﻿/*
 * Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2023-2023. All rights reserved.
 */

#ifndef BURNDLGDFU_H
#define BURNDLGDFU_H

#include <windows.h>
#include <vector>
#include <QSettings>
#include "BurnDlg.h"
#include "DiaglogUSBSetting.h"
#include "ui_BurnDlgDfu.h"
#include "BinFileModel.h"
#include "MultipleBurnDlg.h"

namespace BurnDlg {

class BurnDlgDfu : public BurnDlg {
public:
    explicit BurnDlgDfu(ChipType chipType = ChipType::DFU, QWidget *pParent = nullptr); // 标准构造函数
    ~BurnDlgDfu() override;
    void BurnDlgFuDelete();
    void InitBurnCtrl() override;
    void ResetUIAndFSM() override;
    bool ConfigComPort() override;
    void GetComSettingFromGloble(int &baud, int &databits,
        int &stopbits, QSerialPort::Parity &parity, int &flowctrl) const;
    void SetComSettingToGloble(int baud, int databits, int stopbits, QSerialPort::Parity parity,
        int flowctrl) const;
    void UpdateCmd() const;
    void GetCmdInfoArray(QMap<int, BIN_INFO> &infoArray) const;
    void UpdateList() override;
    bool UpdateList(BIN_INFO_ARRAY &info) const;
    void AnalyseBinFile(QString filePath);
    void JudgeIsSelectAll() const;
    void GetBinInforArray(QMap<int, BIN_INFO> &infoArray, int row) const;
    void GetAllPathFromGrid(std::vector<QString> &path) const;
    bool CheckAllPathInGrid(bool onlyNotify = false);
    bool CheckAllPath(std::vector<QString> &path, bool onlyNotify = false);
    void SaveSettingCfg() const;

protected slots:
    void SlotShowMenuSettings() override;
    void SlotSettingSuccess() override;
    void SlotBnClickedButtonSelectfile() override;
    void SlotCbnSetfocusComboCom();
    void SlotBnClickedButtonConn() override;
    void SlotCheckAutoBurnStateChanged(int state);
    LRESULT SlotSwitchDfu(WPARAM wParam, LPARAM lParam);
    void SlotNMKillfocusListBinPath(const NMHDR *pNMHDR, LRESULT *pResult);
    void SlotLvnItemchangedListBinPath(const QModelIndex &topLeft, const QModelIndex &bottomRight,
                                       const QVector<int> &roles = QVector<int>());
    void SlotInsertPlainText(const QString &editStr) override;
    void SlotModelReset() const;
    void StartBurn();
    void InitializeBurnCtrl();
    void SlotEarseModeCurrentIndexChanged(int index) override;
    void SlotClearPlainText() override;
    void SlotReplacePlainText(const QString &oldText, const QString &newText) override;
    void SlotMergeCurrentCharFormat(const QTextCharFormat &fmt) override;
    void SlotSetProgressValue(unsigned int progress) override;
    void SlotSetProgressRange(int min, int max) override;
    void SlotBnClickedTableHeader(int logicalIndex) override;
    void SlotUpdateList();
    void SlotActionShow() const;
    void SlotBurnFileFinish();
    void SlotDfuModeCurrentTextChanged(QString text);
    void SlotDfuSaveConfig() const;
    void SlotBnClickedMultipleBurn();
    void SlotMultipleBurnDlgClose();
    void SlotCreateNewFwpkgFile();
    void SlotSaveSpiltFwpkgFile();

protected:
    void changeEvent(QEvent* event) override;
private:
    void InitSettingsConnect();
    void InitButtonConnect();
    void InitBurnCtrlConnect();
    void ActionShowActEnable(bool enable) const;
    void InitializeInterfaceControls();
    void InitializeInterfaceControlsAdd();
    void HidEnumerate(QMap <QString, HidDeviceInfo> &mapName);
    static HANDLE OpenDevice(const CHAR *path, BOOL openRw);
    void FillParamAndShow(const QMap<int, BIN_INFO> &info);
    void LoadDlgConfig() const;
    void InitMenus();
    void contextMenuEvent(QContextMenuEvent *event) override;

private:
    Ui::BurnDlgDfu *ui;
    QAction *actImportConfig;
    QAction *actSaveConfig;
    QAction *actSettings;
    QAction *actEnglish;
    QAction *actChinese;
    QAction *actChangeChip;
    QAction *actAbout;
    bool isDfuModelCtrl;
    QMap<QString, HidDeviceInfo> hidShowInfoMap;
    DfuBurnMode dfuMode;
    QSettings *cfg;
    bool isMultipleBurnOpen;
    MultipleBurnDlg *multipleBurnDlg;
    int eraseMode;
    DiaglogUSBSetting *usbSetting;
    QStringList usbLocations;
    QMenu *tableViewMenu;
};

}
#endif
